package com.hunt.service.impl;

import com.hunt.dao.StockztpoolemMapper;
import com.hunt.model.app.Stockztpoolem;
import com.hunt.service.StockZTPoolEmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StockZTPoolEmServiceImpl implements StockZTPoolEmService {
    @Autowired
    StockztpoolemMapper stockztpoolemMapper;

    @Override
    public  List<Stockztpoolem> getstockztpoolem(String type,String orderfield,String sort,String datestr) {
        List<Stockztpoolem> Stockztpoolems = new ArrayList<>();

        if(type.equals("0")){
            Stockztpoolems = stockztpoolemMapper.getAllData0(orderfield,sort,datestr);}
        else if(type.equals("1")){
            Stockztpoolems = stockztpoolemMapper.getAllData1(orderfield,sort,datestr);}
        else if(type.equals("2")){
            Stockztpoolems = stockztpoolemMapper.getAllData2(orderfield,sort,datestr);}
        else if(type.equals("3")){
            Stockztpoolems = stockztpoolemMapper.getAllData3(orderfield,sort,datestr);}
        else if(type.equals("4")){
            Stockztpoolems = stockztpoolemMapper.getAllData4(orderfield,sort,datestr);}
        String isnewstr="0";
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        if(Stockztpoolems.size()==0){
            return Stockztpoolems;
        }
        if(simpleDateFormat.format(date).equals(Stockztpoolems.get(0).getDate())){
            isnewstr="1";
        }
        for(Stockztpoolem s:Stockztpoolems){
            s.setTur(tovariousgrand(s.getTur()));
            s.setCimva(tovariousmillion(s.getCimva()));
            s.setTomva(tovariousmillion(s.getTomva()));
            s.setClfun(tovariousgrand(s.getClfun()));
            s.setReinc(reincformate(s.getReinc()));
            s.setSefun(tovariousgrand(s.getSefun()));
            s.setOnbtu(tovariousgrand(s.getOnbtu()));
            s.setIsnew(isnewstr);
        }

            return Stockztpoolems;
    }

    @Override
    public List<Stockztpoolem> getstockztpoolembydalst(String type, String sort,String datestr) {
        List<Stockztpoolem> Stockztpoolems = new ArrayList<>();

        if(type.equals("0")){
            Stockztpoolems = stockztpoolemMapper.getAllData0ByDalst(sort,datestr);
        }
        else if(type.equals("1")){

            Stockztpoolems = stockztpoolemMapper.getAllData1ByDalst(sort,datestr);}
        else if(type.equals("2")){
            Stockztpoolems = stockztpoolemMapper.getAllData2ByDalst(sort,datestr);}
        else if(type.equals("3")){
            Stockztpoolems = stockztpoolemMapper.getAllData3ByDalst(sort,datestr);}
        else if(type.equals("4")){
            Stockztpoolems = stockztpoolemMapper.getAllData4ByDalst(sort,datestr);}
        String isnewstr="0";
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        if(Stockztpoolems.size()==0){
            return Stockztpoolems;
        }
        if(simpleDateFormat.format(date).equals(Stockztpoolems.get(0).getDate())){
            isnewstr="1";
        }
        for(Stockztpoolem s:Stockztpoolems){
            s.setTur(tovariousgrand(s.getTur()));
            s.setCimva(tovariousmillion(s.getCimva()));
            s.setTomva(tovariousmillion(s.getTomva()));
            s.setClfun(tovariousgrand(s.getClfun()));
            s.setReinc(reincformate(s.getReinc()));
            s.setSefun(tovariousgrand(s.getSefun()));
            s.setOnbtu(tovariousgrand(s.getOnbtu()));
            s.setIsnew(isnewstr);
        }
        return Stockztpoolems;
    }
    //改变为以万  亿 为单位
    private String tovariousgrand(String money){
        if(money==null){
            return "";
        }
        float moneyf=Float.parseFloat(money);
        if(Math.abs(moneyf/10000)>1&&Math.abs(moneyf/100000000)<1){
            float result=moneyf/10000;
            return  String.format("%.2f", result)+"万";
        }
        if(Math.abs(moneyf/100000000)>=1){
            float result=moneyf/100000000;
            return   String.format("%.2f", result)+"亿";
        }
      return  "";
    }
    //改变为以亿 为单位
    private String tovariousmillion(String money){
        if(money==null){
            return "";
        }
        float moneyf=Float.parseFloat(money);
        float result=moneyf/100000000;
        return  String.format("%.2f",result)+"亿";
    }
    //加上%
    private String addhundred(String money){
        if(money==null){
            return "";
        }
     return money+"%";
    }
    //处理入选理由
    private String reincformate(String reinc){
        if(reinc==null){
            return "";
        }
        if(reinc.equals("1")){
            reinc="60日新高";

        }
        if(reinc.equals("2")){
            reinc="近期多日涨停";

        }
        if(reinc.equals("3")){
            reinc="60日新高且近期多次涨停";

        }
        return reinc;
    }
}
